package main;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import java.awt.Component;
import javax.swing.Box;

import com.aptech.mwg.etown.pojo.Account;
import com.aptech.mwg.etown.pojo.exception.InsertDBException;
import com.aptech.mwg.etown.server.business.AccountManager;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Insertacc extends JDialog {

    private final JPanel contentPanel;
    private JTextField txtTxtusers;
    private JTextField txtTxtpass;
    private JTextField txtTxtphone;
    private JTextField txtTxtadd;
    private JTextField txtPos;
    private JTextField txtDate;
    private JTextField txtFirstname;
    private JTextField txtLastname;
    private JTextField txtSocialid;
    private JRadioButton rdbtnMale;
    private JRadioButton rdbtnFemale;
    private JTextField txtCompanyid;
    private JTextField txtCreatedate;
    private final ButtonGroup buttonGroup = new ButtonGroup();
    private JTextField txtEmail;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	try {
	    Insertacc dialog = new Insertacc();
	    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	    dialog.setVisible(false);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Create the dialog.
     */
    public Insertacc() {

	setTitle("Insert Account\r\n");
	setBounds(100, 100, 429, 439);
	contentPanel = new JPanel();
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
	getContentPane().add(contentPanel, BorderLayout.CENTER);
	GridBagLayout gbl_contentPanel = new GridBagLayout();
	gbl_contentPanel.columnWidths = new int[] { 0, 0, 213, 0, 0 };
	gbl_contentPanel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0,
		24, 0, 0, 0, 0 };
	gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0,
		Double.MIN_VALUE };
	gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
		0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
	contentPanel.setLayout(gbl_contentPanel);
	{
	    JLabel lblAcountName = new JLabel("Acount Name: ");
	    GridBagConstraints gbc_lblAcountName = new GridBagConstraints();
	    gbc_lblAcountName.insets = new Insets(0, 0, 5, 5);
	    gbc_lblAcountName.anchor = GridBagConstraints.WEST;
	    gbc_lblAcountName.gridx = 1;
	    gbc_lblAcountName.gridy = 0;
	    contentPanel.add(lblAcountName, gbc_lblAcountName);
	}
	{
	    txtTxtusers = new JTextField();
	    GridBagConstraints gbc_txtTxtusers = new GridBagConstraints();
	    gbc_txtTxtusers.insets = new Insets(0, 0, 5, 5);
	    gbc_txtTxtusers.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtTxtusers.gridx = 2;
	    gbc_txtTxtusers.gridy = 0;
	    contentPanel.add(txtTxtusers, gbc_txtTxtusers);
	    txtTxtusers.setColumns(10);
	}
	{
	    JLabel lblPassword = new JLabel("Password: ");
	    GridBagConstraints gbc_lblPassword = new GridBagConstraints();
	    gbc_lblPassword.anchor = GridBagConstraints.WEST;
	    gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
	    gbc_lblPassword.gridx = 1;
	    gbc_lblPassword.gridy = 1;
	    contentPanel.add(lblPassword, gbc_lblPassword);
	}
	{
	    txtTxtpass = new JTextField();
	    GridBagConstraints gbc_txtTxtpass = new GridBagConstraints();
	    gbc_txtTxtpass.insets = new Insets(0, 0, 5, 5);
	    gbc_txtTxtpass.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtTxtpass.gridx = 2;
	    gbc_txtTxtpass.gridy = 1;
	    contentPanel.add(txtTxtpass, gbc_txtTxtpass);
	    txtTxtpass.setColumns(10);
	}
	{
	    JLabel lblPhone = new JLabel("Phone: ");
	    GridBagConstraints gbc_lblPhone = new GridBagConstraints();
	    gbc_lblPhone.anchor = GridBagConstraints.WEST;
	    gbc_lblPhone.insets = new Insets(0, 0, 5, 5);
	    gbc_lblPhone.gridx = 1;
	    gbc_lblPhone.gridy = 2;
	    contentPanel.add(lblPhone, gbc_lblPhone);
	}
	{
	    txtTxtphone = new JTextField();
	    GridBagConstraints gbc_txtTxtphone = new GridBagConstraints();
	    gbc_txtTxtphone.insets = new Insets(0, 0, 5, 5);
	    gbc_txtTxtphone.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtTxtphone.gridx = 2;
	    gbc_txtTxtphone.gridy = 2;
	    contentPanel.add(txtTxtphone, gbc_txtTxtphone);
	    txtTxtphone.setColumns(10);
	}
	{
	    JLabel lblEmail = new JLabel("Email: ");
	    GridBagConstraints gbc_lblEmail = new GridBagConstraints();
	    gbc_lblEmail.anchor = GridBagConstraints.WEST;
	    gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
	    gbc_lblEmail.gridx = 1;
	    gbc_lblEmail.gridy = 3;
	    contentPanel.add(lblEmail, gbc_lblEmail);
	}
	{
	    txtEmail = new JTextField();
	    GridBagConstraints gbc_txtEmail = new GridBagConstraints();
	    gbc_txtEmail.insets = new Insets(0, 0, 5, 5);
	    gbc_txtEmail.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtEmail.gridx = 2;
	    gbc_txtEmail.gridy = 3;
	    contentPanel.add(txtEmail, gbc_txtEmail);
	    txtEmail.setColumns(10);
	}
	{
	    JLabel lblAddress = new JLabel("Address: ");
	    GridBagConstraints gbc_lblAddress = new GridBagConstraints();
	    gbc_lblAddress.anchor = GridBagConstraints.WEST;
	    gbc_lblAddress.insets = new Insets(0, 0, 5, 5);
	    gbc_lblAddress.gridx = 1;
	    gbc_lblAddress.gridy = 4;
	    contentPanel.add(lblAddress, gbc_lblAddress);
	}
	{
	    txtTxtadd = new JTextField();
	    GridBagConstraints gbc_txtTxtadd = new GridBagConstraints();
	    gbc_txtTxtadd.insets = new Insets(0, 0, 5, 5);
	    gbc_txtTxtadd.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtTxtadd.gridx = 2;
	    gbc_txtTxtadd.gridy = 4;
	    contentPanel.add(txtTxtadd, gbc_txtTxtadd);
	    txtTxtadd.setColumns(10);
	}
	{
	    JLabel lblPosition = new JLabel("Position: ");
	    GridBagConstraints gbc_lblPosition = new GridBagConstraints();
	    gbc_lblPosition.anchor = GridBagConstraints.WEST;
	    gbc_lblPosition.insets = new Insets(0, 0, 5, 5);
	    gbc_lblPosition.gridx = 1;
	    gbc_lblPosition.gridy = 5;
	    contentPanel.add(lblPosition, gbc_lblPosition);
	}
	{
	    txtPos = new JTextField();
	    GridBagConstraints gbc_txtPos = new GridBagConstraints();
	    gbc_txtPos.insets = new Insets(0, 0, 5, 5);
	    gbc_txtPos.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtPos.gridx = 2;
	    gbc_txtPos.gridy = 5;
	    contentPanel.add(txtPos, gbc_txtPos);
	    txtPos.setColumns(10);
	}
	{
	    JLabel lblDateOfBirth = new JLabel("Date of Birth: ");
	    GridBagConstraints gbc_lblDateOfBirth = new GridBagConstraints();
	    gbc_lblDateOfBirth.anchor = GridBagConstraints.WEST;
	    gbc_lblDateOfBirth.insets = new Insets(0, 0, 5, 5);
	    gbc_lblDateOfBirth.gridx = 1;
	    gbc_lblDateOfBirth.gridy = 6;
	    contentPanel.add(lblDateOfBirth, gbc_lblDateOfBirth);
	}
	{
	    txtDate = new JTextField();
	    GridBagConstraints gbc_txtDate = new GridBagConstraints();
	    gbc_txtDate.insets = new Insets(0, 0, 5, 5);
	    gbc_txtDate.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtDate.gridx = 2;
	    gbc_txtDate.gridy = 6;
	    contentPanel.add(txtDate, gbc_txtDate);
	    txtDate.setColumns(10);
	}
	{
	    JLabel lblNewLabel = new JLabel("First Name: ");
	    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
	    gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
	    gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
	    gbc_lblNewLabel.gridx = 1;
	    gbc_lblNewLabel.gridy = 7;
	    contentPanel.add(lblNewLabel, gbc_lblNewLabel);
	}
	{
	    txtFirstname = new JTextField();
	    GridBagConstraints gbc_txtFirstname = new GridBagConstraints();
	    gbc_txtFirstname.insets = new Insets(0, 0, 5, 5);
	    gbc_txtFirstname.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtFirstname.gridx = 2;
	    gbc_txtFirstname.gridy = 7;
	    contentPanel.add(txtFirstname, gbc_txtFirstname);
	    txtFirstname.setColumns(10);
	}
	{
	    JLabel lblLastName = new JLabel("Last Name: ");
	    GridBagConstraints gbc_lblLastName = new GridBagConstraints();
	    gbc_lblLastName.anchor = GridBagConstraints.WEST;
	    gbc_lblLastName.insets = new Insets(0, 0, 5, 5);
	    gbc_lblLastName.gridx = 1;
	    gbc_lblLastName.gridy = 8;
	    contentPanel.add(lblLastName, gbc_lblLastName);
	}
	{
	    txtLastname = new JTextField();
	    GridBagConstraints gbc_txtLastname = new GridBagConstraints();
	    gbc_txtLastname.insets = new Insets(0, 0, 5, 5);
	    gbc_txtLastname.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtLastname.gridx = 2;
	    gbc_txtLastname.gridy = 8;
	    contentPanel.add(txtLastname, gbc_txtLastname);
	    txtLastname.setColumns(10);
	}
	{
	    JLabel lblSex = new JLabel("Sex: ");
	    GridBagConstraints gbc_lblSex = new GridBagConstraints();
	    gbc_lblSex.anchor = GridBagConstraints.WEST;
	    gbc_lblSex.insets = new Insets(0, 0, 5, 5);
	    gbc_lblSex.gridx = 1;
	    gbc_lblSex.gridy = 9;
	    contentPanel.add(lblSex, gbc_lblSex);
	}
	{
	    JPanel panel = new JPanel();
	    GridBagConstraints gbc_panel = new GridBagConstraints();
	    gbc_panel.anchor = GridBagConstraints.WEST;
	    gbc_panel.insets = new Insets(0, 0, 5, 5);
	    gbc_panel.fill = GridBagConstraints.VERTICAL;
	    gbc_panel.gridx = 2;
	    gbc_panel.gridy = 9;
	    contentPanel.add(panel, gbc_panel);
	    panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	    {
		rdbtnMale = new JRadioButton("Male");
		buttonGroup.add(rdbtnMale);
		panel.add(rdbtnMale);
	    }
	    {
		Component horizontalStrut = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut);
	    }
	    {
		rdbtnFemale = new JRadioButton("Female");
		buttonGroup.add(rdbtnFemale);
		panel.add(rdbtnFemale);
	    }
	}
	
	
	{
	    JLabel lblNewLabel_2 = new JLabel("CompanyId: ");
	    GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
	    gbc_lblNewLabel_2.anchor = GridBagConstraints.WEST;
	    gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
	    gbc_lblNewLabel_2.gridx = 1;
	    gbc_lblNewLabel_2.gridy = 11;
	    contentPanel.add(lblNewLabel_2, gbc_lblNewLabel_2);
	}
	{
	    txtCompanyid = new JTextField();
	    GridBagConstraints gbc_txtCompanyid = new GridBagConstraints();
	    gbc_txtCompanyid.insets = new Insets(0, 0, 5, 5);
	    gbc_txtCompanyid.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtCompanyid.gridx = 2;
	    gbc_txtCompanyid.gridy = 11;
	    contentPanel.add(txtCompanyid, gbc_txtCompanyid);
	    txtCompanyid.setColumns(10);
	}
	{
	    JLabel lblCreateDate = new JLabel("Create Date:");
	    GridBagConstraints gbc_lblCreateDate = new GridBagConstraints();
	    gbc_lblCreateDate.anchor = GridBagConstraints.WEST;
	    gbc_lblCreateDate.insets = new Insets(0, 0, 0, 5);
	    gbc_lblCreateDate.gridx = 1;
	    gbc_lblCreateDate.gridy = 12;
	    contentPanel.add(lblCreateDate, gbc_lblCreateDate);
	}
	{
	    txtCreatedate = new JTextField();
	    GridBagConstraints gbc_txtCreatedate = new GridBagConstraints();
	    gbc_txtCreatedate.insets = new Insets(0, 0, 0, 5);
	    gbc_txtCreatedate.fill = GridBagConstraints.HORIZONTAL;
	    gbc_txtCreatedate.gridx = 2;
	    gbc_txtCreatedate.gridy = 12;
	    contentPanel.add(txtCreatedate, gbc_txtCreatedate);
	    txtCreatedate.setColumns(10);
	}
	{
	    JPanel buttonPane = new JPanel();
	    buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
	    getContentPane().add(buttonPane, BorderLayout.SOUTH);
	    {
		JButton okButton = new JButton("OK");
		okButton.addMouseListener(new MouseAdapter() {
		    @Override
		    public void mouseClicked(MouseEvent e) {
			
			AccountManager accmanager = new AccountManager();
			Account account = new Account();
			account.setCompanyId(1);
			account.setLoginName(txtTxtusers.getText());
			account.setPassword(txtTxtpass.getText());
			account.setFirstName(txtFirstname.getText());
			account.setLastName(txtLastname.getText());
			account.setPosition(txtPos.getText());
			account.setEmail(txtEmail.getText());
			account.setPhone(txtTxtphone.getText());
			if (rdbtnMale.isSelected() == true) {
			    account.setSex(1);
			}
			if (rdbtnFemale.isSelected() == true) {
			    account.setSex(0);
			}

			String birthDay = txtDate.getText();
			String createday = txtCreatedate.getText();
			String pattern = "dd/MM/yyyy";
			SimpleDateFormat formmater = new SimpleDateFormat(
				pattern);
			Calendar cal = Calendar.getInstance();
			Calendar cal1 = Calendar.getInstance();
			try {
			    Date date = formmater.parse(birthDay);
			    Date createdate = formmater.parse(createday);
			    cal.setTime(date);
			    cal1.setTime(createdate);
			    String dateStr = formmater.format(date);
			    String create = formmater.format(createdate);
			    System.out.println(dateStr);
			    System.out.println(create);
			    account.setDateOfBirth(cal.getTime());
			    account.setCreate_date(cal1.getTime());
			} catch (ParseException e1) {
			    e1.printStackTrace();
			}
			
			
			try {
			    boolean result = accmanager.insert(account);

			    if (result == true) {
				
				setVisible(false);
				
			    } else {

			    }
			    System.out.println("Insert result:" + result);
			} catch (InsertDBException e1) {
			    System.err.println("Can not insert account by : "
				    + e1.getMessage());
			    e1.printStackTrace();
			}
			
			accManager acc = new accManager();
			acc.loadtable();
			
			
		    }
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
	    }
	    {
		JButton btnReset = new JButton("Reset");
		btnReset.addMouseListener(new MouseAdapter() {
		    @Override
		    public void mouseClicked(MouseEvent e) {
			txtDate.setText(null);
			txtFirstname.setText(null);
			txtLastname.setText(null);
			txtPos.setText(null);
			txtSocialid.setText(null);
			txtTxtadd.setText(null);
			txtTxtpass.setText(null);
			txtTxtphone.setText(null);
			txtTxtusers.setText(null);
			txtCompanyid.setText(null);
			txtCreatedate.setText(null);
			rdbtnMale.setSelected(false);
			rdbtnFemale.setSelected(false);
			
		    }
		});
		buttonPane.add(btnReset);
	    }
	    {
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			setVisible(false);
		    }
		});
		buttonPane.add(btnCancel);
	    }
	}
    }

}
